package com.biglead.userservice.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biglead.common.pojo.UserInfo;
import com.biglead.userservice.mapper.UserMapper;
import com.biglead.userservice.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, UserInfo> implements UserService {
    /**
     * 查询用户详情
     *
     * @param id
     */
    @Override
    public UserInfo queryById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询用户详情
     *
     * @param userName
     */
    @Override
    public UserInfo queryByUserName(String userName) {
        return this.getOne(new QueryWrapper<UserInfo>().eq("username", userName));
    }

    /**
     * 根据 openId 查询用户详情
     *
     * @param map
     */
    @Override
    public UserInfo queryByOpenId(Map<String, Object> map) {

        String openId = map.get("openId").toString();

        String phone = null;
        if (map.get("phone") != null) {
            phone = map.get("phone").toString();
        }
        UserInfo one = this.getOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getOpenId, openId));
        if (one == null) {

            String sessionKey = map.get("sessionKey").toString();
            String unionId = null;
            String nickName = null;
            String avatarUrl = null;

            if (map.get("unionId") != null) {
                unionId = map.get("unionId").toString();
            }
            if (map.get("nickName") != null) {
                nickName = map.get("nickName").toString();
            }

            if (map.get("avatarUrl") != null) {
                avatarUrl = map.get("avatarUrl").toString();
            }


            //注册用户
            UserInfo userInfo = new UserInfo();
            userInfo.setUsername(openId);
            userInfo.setOpenId(openId);
            userInfo.setNickName(nickName);
            userInfo.setUnionId(unionId);
            userInfo.setUserPhone(phone);
            userInfo.setStatus(1);
            userInfo.setIcon(avatarUrl);
            userInfo.setCreateTime(new Date());


            //将密码进行加密操作
            BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
            String encodePassword = bcryptPasswordEncoder.encode(openId);
            userInfo.setPassword(encodePassword);

            this.save(userInfo);

            return userInfo;
        }
        if(StringUtils.isNotEmpty(phone)) {
            String userPhone = one.getUserPhone();
            if (StringUtils.isEmpty(userPhone) || !userPhone.equals(phone)){
                one.setUserPhone(phone);
                this.updateById(one);
            }
        }
        return one;
    }
}
